<?php
 require_once("../inc/sku.php");
 require_once("../inc/inventory.php");
 
 $sku = getSKU($_GET['id']);
 $price_results = priceHistory($_GET['id']);
 $stock = getInventory($_GET['id']);
 if($stock == null)
 {
 	$stock = 0;
 }
 
?>
<!DOCTYPE html>
    <html>
    <head>
        <meta charset="utf-8" />
        <title><?=$title ?> Product Details: <?=$sku['vendor']?> <?=$sku['product_name']?></title>
         <? require('../inc/header-info.php'); ?>
    </head>
    <body>
        <div class="container">
            <? include('../inc/nav.php'); ?>
            
            
            <? if(isset($_GET['updated'])): ?>
            	<div class="alert alert-success">
                	<button class="close">x</button>
                    	Product Updated.
                </div>
            <? endif; ?>
            
            <? if(isset($_GET['inventory'])): ?>
            	<div class="alert alert-success">
                	<button class="close">x</button>
                    	Inventory Added.
                </div>
            <? endif; ?>
            
             	<h4>Product Details</h4>
             	
             	<a class="edit-product btn btn-mini btn-primary" href="<?=$url?>sku/addsku.php?id=<?=$sku['id']?>">Edit Product</a>
             	<a class="add-inventory btn btn-mini btn-primary" href="<?=$url?>inventory/addinventory.php?id=<?=$sku['id']?>">Add Inventory</a>
             	
             	<dl class="dl-horizontal">
             		<dt>SKU:</dt>
  					<dd><?=$sku['id']?></dd>
  					<dt>Vendor:</dt>
  					<dd><?=$sku['vendor']?></dd>
  					<dt>Product Name:</dt>
  					<dd><?=$sku['product_name']?></dd>
  					<dt>Product Description:</dt>
  					<dd><?=$sku['description']?></dd>
  					<dt>Product Weight:</dt>
  					<dd><?=$sku['weight']?></dd>
  					<dt>Inventory:</dt>
  					<dd><?=$stock?></dd>
  					<dt>Available:</dt>
  					<dd><?=$sku['weight']?></dd>
				</dl>
    	</div>
		<div class="container">
            <h4>Price History Information for SKU: <?=$sku['id']?></h4>
            <a class="new-price btn btn-mini btn-primary" href="<?=$url?>sku/addprice.php?id=<?=$sku['id']?>">Change Price</a>
            
            <? if(isset($_GET['newprice'])): ?>
            	<div class="alert alert-success">
                	<button class="close">x</button>
                    	New Price.
                </div>
            <? endif; ?>
            
            <table class="table">
    			<thead>
    				<tr>
    					<th>Price</th>
    					<th>Date</th>
    				</tr>
    			</thead>
    			<tbody>
    				<? while($price = $price_results->fetch_assoc()): ?>
    					<tr>
							<td>$ <?=$price['price']?></td>
							<td><?=$price['date']?></td>
                        </tr>
                    <? endwhile; ?>

    			</tbody>
    		</table>
		</div>
		<div id="modal-com" class="modal hide fade">
			<div class="modal-header">
				<button type="button" class="close" data-dismiss="modal" aria-hidden="true">
					&times;
				</button>
				<h3 class='link-type'></h3>
			</div>
			<div class="modal-body">

			</div>
			<div class="modal-footer"></div>
		</div>
		<script type="text/javascript" src="http://jzaefferer.github.com/jquery-validation/jquery.validate.js"></script>
		<script>
			$(function() {

				// Calls the New Modal
				$(".new-price").click(function(){
					$.get(this.href, function(data){
						$(".modal-body").html($(data).find("form"));
					})
					$(".link-type").append("New Price");
					$('#modal-com').modal('show');
					
					return false;
				});
				
				$(".add-inventory").click(function(){
					$.get(this.href, function(data){
						$(".modal-body").html($(data).find("form"));
					})
					$(".link-type").append("Adjust Inventory");
					$('#modal-com').modal('show');
					
					return false;
				});

				// Calls the Edit Modal
				$(".edit-product").click(function(){
					$.get(this.href, function(data){
						$(".modal-body").html($(data).find("form"));
					})
					$(".link-type").append("Edit Product");
					$('#modal-com').modal('show');
					
					return false;
				});

				//Post the Form Back to the server
				$("form").submit(function() {
					$("form").validate();
					var postData = $(this).serialize();
					$.post(this.action. postData + '&ajax=true');
					
				});
				
				//Function to close and clear the modal
				$(".close").click(function(){
					$(".modal-body").empty();
					$(".link-type").empty();
					$('#modal-com').modal('hide');
					return false;
				});
				
				//---- Close the alert with the X
				$(".close").click(function(){
					$(this).closest(".alert").slideUp();
				});
	
				//---- Auto Close the New Inventory Confirmation Alert
				<? if(isset($_GET['inventory'])): ?>
					$(".alert").delay(2000).slideUp();
				<? endif; ?>
				
				//---- Auto Close the New Price Confirmation Alert
				<? if(isset($_GET['newprice'])): ?>
					$(".alert").delay(2000).slideUp();
				<? endif; ?>
				
				//---- Auto Close the Update Confirmation Alert
				<? if(isset($_GET['updated'])): ?>
					$(".alert").delay(2000).slideUp();
				<? endif; ?>
			});
		</script>
	</body>
</html>